import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/*
 * 通用卡片组件
 * 左側圖片，右側內容，
 * 右側內容頭部始終是標題，下方內容組件會有變化
 */
class UniversalCard extends StatelessWidget {
//  圖片URL
  final String url;

//  標題
  final String title;

//  主要內容
  final List<Widget> children;

//  點擊事件
  final VoidCallback onPressed;
  final bool noImage;

  //右下角控件
  final Widget childRightWidget;

  const UniversalCard(
      {Key key,
      this.url,
      @required this.title,
      @required this.children,
      this.onPressed,
      this.noImage = false,
      this.childRightWidget})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onPressed,
      child: Container(
//        height: ScreenUtil().setHeight(150),
        width: ScreenUtil().setWidth(343),
        padding: EdgeInsets.all(5),
        margin: EdgeInsets.only(top: 10, bottom: 10, left: 15, right: 15),
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(20)),
            boxShadow: [
              BoxShadow(
                  color: Color(0xffe3e8ed), blurRadius: 3.0, spreadRadius: 2.0),
              BoxShadow(
                  color: Color(0xffe7ebf0), blurRadius: 3.0, spreadRadius: 2.0),
              BoxShadow(
                  color: Color(0xffebeef2), blurRadius: 3.0, spreadRadius: 2.0),
              BoxShadow(
                  color: Color(0xffeff1f5), blurRadius: 3.0, spreadRadius: 2.0)
            ]),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Row(
//          mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                noImage == true ? Container() : CourseImage(image: url),
                Expanded(
                  child: CurseInfo(),
                )
              ],
            ),
            childRightWidget!=null?Container(
              margin: EdgeInsets.only(right: 20, bottom: 10),
              child: childRightWidget,
            ):Container()
          ],
        ),
      ),
    );
  }

  //  图片
  Widget CourseImage({@required String image, bool teaching = false}) {
    return Stack(
      children: <Widget>[
        Container(
            width: ScreenUtil().setWidth(128),
            height: ScreenUtil().setHeight(128),
            margin: EdgeInsets.all(10),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(20)),
                image: DecorationImage(
                    image: url == null
                        ? AssetImage("images/noimg.jpg")
                        : NetworkImage(image),
                    fit: BoxFit.cover))),
        teaching == true
            ? Container(
                margin: EdgeInsets.only(top: 20),
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    color: Color(0xff93BB24),
                    borderRadius: BorderRadius.only(
                        topRight: Radius.circular(25),
                        bottomRight: Radius.circular(25))),
                height: ScreenUtil().setHeight(24),
                width: ScreenUtil().setWidth(71),
                child: Text(
                  "持教課程",
                  style: TextStyle(color: Colors.white),
                  textAlign: TextAlign.center,
                ),
              )
            : Container(),
      ],
    );
  }

  //  文字详情
  Widget CurseInfo() {
    return Container(
      alignment: Alignment.topLeft,
      padding: EdgeInsets.only(left: 10, top: 10),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text(
            title,
            maxLines: 4,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
                color: Color(0xff154D7C),
                fontSize: ScreenUtil().setSp(15),
                fontWeight: FontWeight.w500),
          ),
          Column(
            children: children,
          )
        ],
      ),
    );
  }
}
